package com.cloud.school.bus.teacherhelper.modules.home;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshListView;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase.Mode;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.base.activity.BaseActivity;
import com.cloud.school.bus.teacherhelper.base.database.AllPictureIgnoreDB;
import com.cloud.school.bus.teacherhelper.base.database.TeacherPictureIgnoreDB;
import com.cloud.school.bus.teacherhelper.base.database.UploadFileDB;
import com.cloud.school.bus.teacherhelper.base.dialog.CustomAlertDialog;
import com.cloud.school.bus.teacherhelper.base.view.TitleNavBarView;
import com.cloud.school.bus.teacherhelper.entitys.Picture;
import com.cloud.school.bus.teacherhelper.entitys.UploadFile;
import com.cloud.school.bus.teacherhelper.modules.SlidingActivity;
import com.cloud.school.bus.teacherhelper.modules.home.adapter.VideoSelectedAdapter;

/**
 * 选择视频界面
 * 
 * @author jiahongfei
 * 
 */
public class VideoSelectedActivity extends BaseActivity {

	private TitleNavBarView titleNavBarView;
	private PullToRefreshListView mPullToRefreshListView;
	private VideoSelectedAdapter mVideoSelectedAdapter;
	private ViewGroup mNoDataLayout;

	private String mTitleString;
	private List<Picture> mPictures;
	/** 2表示视频草稿箱 */
	private int mFlag = 0;
	private int mCount = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_video_selected);

		init();
	}

	@Override
	protected void init() {

		mFlag = getIntent().getIntExtra("flag", 0);
		mTitleString = getIntent().getStringExtra("title");
		mPictures = (List<Picture>) getIntent()
				.getSerializableExtra("Pictures");
		mCount = mPictures.size();

		mNoDataLayout = (ViewGroup)findViewById(R.id.noDataLayout);
		mPullToRefreshListView = (PullToRefreshListView) findViewById(R.id.listView);
		mPullToRefreshListView.setMode(Mode.DISABLED);
		mVideoSelectedAdapter = new VideoSelectedAdapter(mContext, mPictures);
		mPullToRefreshListView.setAdapter(mVideoSelectedAdapter);
		
		if(0 == mPictures.size()){
			mNoDataLayout.setVisibility(View.VISIBLE);
			mPullToRefreshListView.setVisibility(View.GONE);
		}else {
			mNoDataLayout.setVisibility(View.GONE);
			mPullToRefreshListView.setVisibility(View.VISIBLE);
		}

		setListener();
		setTitleNavBar();
	}

	@Override
	protected void setTitleNavBar() {
		titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage(mTitleString);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				cancel();
			}
		});
		setTitleRightButtonState();
		titleNavBarView.setRightButtonImageString(R.drawable.btn_send_xml, getString(R.string.edit), android.R.color.white);
		titleNavBarView.setRightButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (mVideoSelectedAdapter.getDeleteUploadFile()) {
					// 弹出删除对话框
					titleNavBarView.setRightButtonImageString(R.drawable.btn_send_xml, getString(R.string.edit), android.R.color.white);
					mVideoSelectedAdapter.setDeleteUploadFile(false);
					mVideoSelectedAdapter.notifyDataSetChanged();
				} else {
					titleNavBarView.setRightButtonImageString(R.drawable.btn_send_xml, getString(R.string.complete), android.R.color.white);
					mVideoSelectedAdapter.setDeleteUploadFile(true);
					mVideoSelectedAdapter.notifyDataSetChanged();
				}
			}
		});
	}
	
	private void setTitleRightButtonState() {
		if(0 == mPictures.size()){
			titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
		}else {
			titleNavBarView.setRightButtonVisibility(View.VISIBLE);
		}
	}

	private void showDeleteDialog(final TitleNavBarView titleNavBarView,
			final Picture picture) {
		CustomAlertDialog customAlertDialog = new CustomAlertDialog(mContext,
				R.style.CustomAlertDialog);
		customAlertDialog.setTitleMessage(getString(R.string.tips), Color.BLACK);
		customAlertDialog.setMessage(getString(R.string.delete_the_video));
		customAlertDialog.setLeftButton(getString(R.string.delete), new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				// 删除本地视频
				File file = new File(picture.getPicturePath().replace(
						"file:///", "/"));
				file.delete();

				// 删除列表
				mPictures.remove(picture);

				if (0 == mPictures.size()) {
					titleNavBarView.setRightButtonImageString(R.drawable.btn_send_xml, getString(R.string.edit), android.R.color.white);
					mVideoSelectedAdapter.setDeleteUploadFile(false);
				}
				mVideoSelectedAdapter.notifyDataSetChanged();
				
				if(0 == mPictures.size()){
					mNoDataLayout.setVisibility(View.VISIBLE);
					mPullToRefreshListView.setVisibility(View.GONE);
				}else {
					mNoDataLayout.setVisibility(View.GONE);
					mPullToRefreshListView.setVisibility(View.VISIBLE);
				}
				setTitleRightButtonState();
			}
		});
		customAlertDialog.setRightButton(getString(R.string.cancel), new OnClickListener() {

			@Override
			public void onClick(View arg0) {

			}
		});
		customAlertDialog.show();
	}

	private void cancel() {
		if (mCount != mPictures.size()) {
			Intent intent = new Intent();
			intent.putExtra("pictureList", (ArrayList<Picture>) mPictures);
			intent.putExtra("flag", mFlag);
			setResult(RESULT_OK, intent);
		}
		finish();
	}

	@Override
	protected void setListener() {
		mPullToRefreshListView
				.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						Picture picture = mPictures.get(arg2);
						if (mVideoSelectedAdapter.getDeleteUploadFile()) {
							// 删除
							showDeleteDialog(titleNavBarView, picture);
						} else {
							Intent intent = new Intent(mContext,
									UploadFileStuSelectedActivity.class);
							intent.putExtra("title", getString(R.string.upload_video));
							intent.putExtra("index", arg2);
							intent.putExtra("picture", picture);
							startActivityForResult(intent, 0);
						}
					}
				});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 0: {
			// 上传视频返回
			switch (resultCode) {
			case RESULT_OK: {
				if(null != data){
					int index = data.getIntExtra("index", 0);
					mPictures.remove(index);
					mVideoSelectedAdapter.notifyDataSetChanged();
					setTitleRightButtonState();
					if(0 == mPictures.size()){
						mNoDataLayout.setVisibility(View.VISIBLE);
						mPullToRefreshListView.setVisibility(View.GONE);
					}else {
						mNoDataLayout.setVisibility(View.GONE);
						mPullToRefreshListView.setVisibility(View.VISIBLE);
					}
				}
				break;
			}
			case RESULT_CANCELED: {
				break;
			}
			default:
				break;
			}
			break;
		}
		default:
			break;
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			cancel();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
